package ua.vip.easyportfolio.dao;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.GphotoFeed;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ua.vip.easyportfolio.service.EMF;
import ua.vip.easyportfolio.service.PicasawebClient;

import javax.persistence.EntityManager;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.List;

/**
 * User: viktorp
 * Date: 25.03.2010
 * Time: 22:03:13
 */
public class PicassaPhotoDao implements IPhotoDao {
    public static final Logger LOGGER = LoggerFactory.getLogger(PicasawebClient.class);

    private static final String API_PREFIX
            = "http://picasaweb.google.com/data/feed/api/user/";

    private final PicasawebService service;

    /**
     * Constructs a new un-authenticated client.
     */
    public PicassaPhotoDao(String applicationName) {
        this(new PicasawebService(applicationName), null, null);
    }

    /**
     * Constructs a new client with the given username and password.
     */
    public PicassaPhotoDao(PicasawebService service, String uname,
                           String passwd) {
        this.service = service;

        if (uname != null && passwd != null) {
            try {
                service.setUserCredentials(uname, passwd);
            } catch (AuthenticationException e) {
                throw new IllegalArgumentException(
                        "Illegal username/password combination.");
            }
        }
    }

    /**
     * Retrieves the albums for the given user.
     */
    @Override
    public List<AlbumEntry> getAlbums(String username) throws IOException,
            ServiceException {
        String albumUrl = API_PREFIX + username + "?kind=album";
        return getFeed(albumUrl, UserFeed.class).getAlbumEntries();
    }

    @Override
    public Collection<AlbumEntry> getPortfolioAlbums(String login) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            List resultList = em.createQuery(
                    "SELECT c FROM PortfolioAlbum c WHERE c.login LIKE :login")
                    .setParameter("login", login)
                    .getResultList();
        } finally {
            em.close();
        }
        return null;
    }

    /**
     * Helper function to allow retrieval of a feed by string url, which will
     * create the URL object for you.  Most of the Link objects have a string
     * href which must be converted into a URL by hand, this does the conversion.
     */
    public <T extends GphotoFeed> T getFeed(String feedHref,
                                            Class<T> feedClass) throws IOException, ServiceException {
        System.out.println("Get Feed URL: " + feedHref);
        return service.getFeed(new URL(feedHref), feedClass);
    }

}
